﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Net.Mail;
using System.Net;
using System.Threading;

namespace DNA
{
    public partial class DNA : Form
    {
        private Microsoft.Office.Interop.Excel.Application ObjExcel;
        private Microsoft.Office.Interop.Excel.Workbook ObjWorkBook;
        private Microsoft.Office.Interop.Excel.Worksheet ObjWorkSheet;
        public char A, G, T, C, D, I;//избавиться от них
        public bool set_val_nukl = false;
        public Int32 rows = 0;
        public bool flag=false;//открыта ли база
        public bool preobr_flag = false;//преобразованны ли значения
   
        public DNA()
        {
            InitializeComponent();
            TDNA.Rows.Clear();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            bool iscolums = false;
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Файл Excel|*.XLSX;*.XLS";
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    TDNA.Columns.Clear();
                    ObjExcel = new Microsoft.Office.Interop.Excel.Application();
                    ObjWorkBook = ObjExcel.Workbooks.Open(openDialog.FileName);
                    ObjWorkSheet = ObjExcel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
                    Microsoft.Office.Interop.Excel.Range rg = null;

                    Int32 row = 1;
                    TDNA.Rows.Clear();
                    List<String> arr = new List<string>();
                    List<String> arrr = new List<string>();
                    while (ObjWorkSheet.get_Range("a" + row, "a" + row).Value != null)
                    {
                        rg = ObjWorkSheet.get_Range("a" + row, "z" + row);
                        foreach (Microsoft.Office.Interop.Excel.Range item in rg)
                        {
                            try
                            {
                                arr.Add(item.Value.ToString().Trim());
                            }
                            catch { arr.Add(""); }
                        }
                        if (iscolums == false)
                        {
                            for (int i = 0; i < arr.Count; i++)
                            {
                                DataGridViewColumn data = new DataGridViewTextBoxColumn();
                                TDNA.Columns.Add(data);
                                TDNA.Columns[i].HeaderText = arr[i];
                            }
                            iscolums = true;
                            goto LoopLabel;
                        }
                        TDNA.Rows.Add(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8], arr[9], arr[10], arr[11], arr[12], arr[13], arr[14], arr[15], arr[16], arr[17], arr[18], arr[19], arr[20], arr[21], arr[22], arr[23], arr[24], arr[25]);
                    LoopLabel:
                        arr.Clear();
                        row++;
                        rows = row-1;
                    }
                    flag = true;
                }
                catch (Exception ex) { MessageBox.Show("Ошибка: " + ex.Message, "Ошибка при считывании excel файла", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                finally
                {
                    if (flag == true)
                    {
                        ObjWorkBook.Close(false, "", null);
                        ObjExcel.Quit();
                        ObjWorkBook = null;
                        ObjWorkSheet = null;
                        ObjExcel = null;
                        GC.Collect();
                    }
                }
            }
        }

        private void преобразоватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void значенияAGTCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            value_AGTC newval = new value_AGTC();
            newval.Owner = this;
            newval.Show();
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void preobrazvat()
        {
            if (set_val_nukl == false)
            {
                for (int i = 0; i <= 25; i++)
                {
                    for (int j = 0; j < rows - 1; j++)
                    {
                        TDNA[i, j].Value = Change_val.preobraz(TDNA[i, j].Value.ToString());
                    }
                }
            }
            else
            {
                for (int i = 0; i <= 25; i++)
                {
                    for (int j = 0; j < rows - 1; j++)
                    {
                        TDNA[i, j].Value = Change_val.preobraz(TDNA[i, j].Value.ToString(),A,G,T,C,D,I);
                    }
                }
            }
            preobr_flag = true;
        }

        private void преобразоватьВЧисловойФорматToolStripMenuItem_Click(object sender, EventArgs e)
        {
            preobrazvat();
        }

        private void otmena()
        {
            if (set_val_nukl == false)
            {
                for (int i = 0; i <= 25; i++)
                {
                    for (int j = 0; j < rows - 1; j++)
                    {
                        if (TDNA.Columns[i].HeaderText == "ожирение" || TDNA.Columns[i].HeaderText == "курение" || TDNA.Columns[i].HeaderText == "алкоголь" || TDNA.Columns[i].HeaderText == "возраст") continue;
                        TDNA[i, j].Value = Change_val.vernyt(TDNA[i, j].Value.ToString());
                    }
                }
            }
            else
            {
                for (int i = 0; i <= 25; i++)
                {
                    for (int j = 0; j < rows - 1; j++)
                    {
                        if (TDNA.Columns[i].HeaderText == "ожирение" || TDNA.Columns[i].HeaderText == "курение" || TDNA.Columns[i].HeaderText == "алкоголь" || TDNA.Columns[i].HeaderText == "возраст") continue;
                        TDNA[i, j].Value = Change_val.vernyt(TDNA[i, j].Value.ToString(), A, G, T, C, D, I);
                    }
                }
            }
            preobr_flag = false;
        }


        private void отменаПреобразованийToolStripMenuItem_Click(object sender, EventArgs e)
        {
            otmena();
        }

        private void корреляционныйАнализToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                if (preobr_flag == true)
                {
                    corel newcorel = new corel();
                    newcorel.Owner = this;
                    newcorel.fillcomuns();
                    newcorel.Show();
                }
                else MessageBox.Show("Данные не преобразованны!");                
            }
            else MessageBox.Show("Нет данных!");
        }

        private void регрессионныйАнализToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
             if (preobr_flag == true)
                {
                    regres reg = new regres();
                    reg.Owner = this;
                    reg.fillcomuns();
                    reg.Show();
                }
             else MessageBox.Show("Данные не преобразованны!");     
             }
            else MessageBox.Show("Нет данных!");
        }

        private void zadat_Click(object sender, EventArgs e)
        {
            value_AGTC newval = new value_AGTC();
            newval.Owner = this;
            newval.Show();
        }

        private void preobr_Click(object sender, EventArgs e)
        {
            preobrazvat();
        }

        private void corel_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                if (preobr_flag == true)
                {
                    corel newcorel = new corel();
                    newcorel.Owner = this;
                    newcorel.fillcomuns();
                    newcorel.Show();
                }
                else MessageBox.Show("Данные не преобразованны!"); 
            }
            else MessageBox.Show("Нет данных!");
        }

        private void regres_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                if (preobr_flag == true)
                {
                    regres reg = new regres();
                    reg.Owner = this;
                    reg.fillcomuns();
                    reg.Show();
                }
                else MessageBox.Show("Данные не преобразованны!"); 
            }
            else MessageBox.Show("Нет данных!");
        }

        private void данныеToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void деревоРешенийToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (flag == true)
            {
            tree maletree = new tree();
            maletree.Owner = this;
            maletree.fill_atr();
            maletree.Show();
            }
             else MessageBox.Show("Нет данных!");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            otmena();
        }
    }
}
